

package org.hive.hive;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class camera extends Activity {
    private static final int IMAGE_CAPTURE = 0;
    private Button startBtn;
    private Button upldBtn;
    private Uri imageUri;
    private ImageView imageView;

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        imageView = (ImageView)findViewById(R.id.img);
        startBtn = (Button) findViewById(R.id.startBtn);
        upldBtn = (Button) findViewById(R.id.upldBtn);
        startBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	//UploadImage();
                startCamera();
              //  UploadImage();
            }
        });
        upldBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            
                
               UploadImage();
            }
        });
    }

    public void startCamera() {
        Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
        String fileName = "testphoto5.jpg";
        ContentValues values = new ContentValues();
        //values.put(MediaStore.Images.Media.TITLE, fileName);
        values.put(MediaStore.Images.Media.DESCRIPTION,
                "Image capture by camera");
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        imageUri = getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(intent, IMAGE_CAPTURE);
       
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == IMAGE_CAPTURE) {
            if (resultCode == RESULT_OK){
                //Log.d("ANDRO_CAMERA","Picture taken!!!");
            	
                imageView.setImageURI(imageUri);
               
                ///call the mobilephotoupload. get the byte array..
               // UploadImage();
                
            }
        }
    }
    protected boolean UploadImage()
    {
    	final AlertDialog alertDialog = new AlertDialog.Builder(camera.this).create();
    	try{
    		
    		
    		
    		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			      public void onClick(DialogInterface dialog, int which ) {
			      }
			      });
    		alertDialog.setMessage("Uploading Images");
    		alertDialog.show();
    		File f = new File("/sdcard/dcim/camera/");
    		File[] listfiles = f.listFiles();
    		FileInputStream fis;
    		fis = new FileInputStream(listfiles[listfiles.length-1]);
    		long length  = listfiles[listfiles.length-1].length();
    		alertDialog.setMessage(Long.toString(length));
    		alertDialog.show();
    		int albumid = (Integer)Global.albumlist.get(Global.curr_album_name);
    		int cat_id = Global.curr_cat_id;
    		String URL = Global.URL+"MobilePhotoUpload?userid=" + Global.userid + "&albumid=" + albumid + "&categoryid=" + cat_id + "&lat=" + Global.curr_lat + "&long="+Global.curr_lon;
    		alertDialog.setMessage(URL);
    		alertDialog.show();
    		InputStreamEntity reqEntity = new InputStreamEntity(fis,length);
    		HttpClient hc = new DefaultHttpClient();
    		HttpPost post = new HttpPost(URL);
    		post.setEntity(reqEntity);
    		reqEntity.setContentType("image/jpg");
    		HttpResponse rp = hc.execute(post);
    		InputStream in = rp.getEntity().getContent();
    		int photo_id = Integer.parseInt(IOUtils.toString(in));
    		photos tempPhoto = new photos();
    		tempPhoto.photoid = photo_id;
    		tempPhoto.albumid = albumid;
    		tempPhoto.lat = (float)Global.curr_lat;
    		tempPhoto.lon = (float)Global.curr_lon;
    		tempPhoto.owner_id = Global.userid;
    		Global.takenPhotos.put(new Integer(photo_id), tempPhoto);
    		/*long currsecs = System.currentTimeMillis();
    		String currtime = Long.toString(currsecs);
    		File newFile = new File("/sdcard/dcim/camera/"+currtime);
    		listfiles[listfiles.length-1].renameTo(newFile);*/
    	}
    	catch (Exception e)
    	{
    		alertDialog.setMessage("Caught Exception: " + e.getMessage() );
    		alertDialog.show();
    		e.printStackTrace(); 
    		return false;

    	}
    	
    	
    	return true;
    }
   
}